import fs from 'fs';
import path from 'path';

export class FileUtil {

    static readFile(filePath: string) {
        let result = '';
        if (fs.existsSync(filePath)) {
            result = fs.readFileSync(filePath, 'utf-8');
        }
        return result;
    }

    static writeFile(filePath: string, data: string) {
        try {
            fs.writeFileSync(filePath, data);
        } catch (error) {
            console.error('write file error-->', filePath);
        }
    }

    static deepMakeDir(dirPath: string): boolean {
        if (fs.existsSync(dirPath)) {
            return true;
        }
        if (FileUtil.deepMakeDir(path.dirname(dirPath))) {
            fs.mkdirSync(dirPath);
            return true;
        }
        return false;
    }

    static recursiveMakeDir(dirPath: string) {
        if (fs.existsSync(dirPath)) {
            return;
        }
        fs.mkdirSync(dirPath, { recursive: true });
    }
}
